package com.yomahub.liteflow.example.transform;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.info.VideoInfo;
import ws.schild.jave.info.VideoSize;

import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class VideoUtil {
    private static Map<String, Integer> sizeBitRateMap;
    //视频最大宽度
    private final static int maxWidth = 600;
    //视频最大高度
    private final static int maxHeight = 1024;

    static {
        sizeBitRateMap = new HashMap<>();
        sizeBitRateMap.put("1920*1080", 4992);
        sizeBitRateMap.put("1280*720", 2496);
        sizeBitRateMap.put("1024*576", 1856);
        sizeBitRateMap.put("840*480", 1216);
        sizeBitRateMap.put("768*432", 1088);
        sizeBitRateMap.put("640*360", 896);
        sizeBitRateMap.put("424*240", 576);
    }


    public static void main(String[] args) {
        VideoUtil videoUtils = new VideoUtil();
        videoUtils.convertVideoToMP4(new File(
                        "D:\\WeChat_20230531104437.mp4"),
                "D:\\aaaa.mp4");



    }
    /**
     * @param source     源文件
     * @param targetPath 转码后的路径
     */
    public static void convertVideoToMP4(File source, String targetPath) {
        MultimediaObject multimediaObject = new MultimediaObject(source);
        try {
            MultimediaInfo info = multimediaObject.getInfo();
            VideoInfo videoInfo = info.getVideo();
            VideoSize size = videoInfo.getSize();
            Integer owith = size.getWidth();
            Integer oheight = size.getHeight();
            System.out.println("原视频宽：" + size.getWidth());
            System.out.println("原视频高：" + size.getHeight());
            System.out.println("原视频比特率：" + videoInfo.getBitRate() / 1000);
            System.out.println("原视频编码：" + videoInfo.getDecoder());


            Integer bitRate = sizeBitRateMap.get(size.getWidth() + "*" + size.getHeight());
            VideoAttributes video = new VideoAttributes();
            //设置视频编码
            video.setCodec("h264");
            if (ObjectUtil.isNotNull(bitRate)) {
                //设置比特率
                video.setBitRate(bitRate * 1000);
            }
            //设置分辨率(最大比例600*1024)
            if (owith>maxWidth||oheight>maxHeight){
                int dwidth = 0;
                int dheight = 0;
                //超宽
                if (owith>maxWidth&&oheight<=maxHeight){
                    dwidth = maxWidth;
                    dheight = maxWidth*oheight/owith;
                }
                //超高
                if (owith<=maxWidth&&oheight>maxHeight){
                    dheight = maxHeight;
                    dwidth = maxHeight*owith/oheight;
                }
                //超宽又超高
                if (owith>600&&oheight>1024){
                    //宽度比例大
                    if ((owith/oheight)>(maxWidth/maxHeight)){
                        dwidth = maxWidth;
                        dheight = maxWidth*oheight/owith;
                    }else {
                        dheight = maxHeight;
                        dwidth = maxHeight*owith/oheight;
                    }
                }
                dwidth = NumberUtil.isEven(dwidth)?dwidth:dwidth-1;
                dheight = NumberUtil.isEven(dheight)?dheight:dheight-1;
                VideoSize vsize = new VideoSize(dwidth,dheight);
                video.setSize(vsize);
            }
            File target = new File(targetPath);
            AudioAttributes audio = new AudioAttributes();
            //设置编码器名称
            audio.setCodec("aac");
            EncodingAttributes attrs = new EncodingAttributes();
            //设置转换后的格式
            attrs.setOutputFormat("mp4");
            attrs.setAudioAttributes(audio);
            attrs.setVideoAttributes(video);
            //多线程转码
            attrs.setEncodingThreads(Runtime.getRuntime().availableProcessors()/2);
            Encoder encoder = new Encoder();
            encoder.encode(multimediaObject, target, attrs);
            //花费毫秒数
            MultimediaObject multimediaObjectOfter = new MultimediaObject(Paths.get(targetPath).toFile());
            MultimediaInfo info1 = multimediaObjectOfter.getInfo();
            VideoInfo video1 = info1.getVideo();
            VideoSize size1 = video1.getSize();

            System.out.println("转换后视频宽：" + size1.getWidth());
            System.out.println("转换后视频高：" + size1.getHeight());
            System.out.println("转换后视频比特率：" + video1.getBitRate() / 1000);
            System.out.println("转换后视频编码：" + video1.getDecoder());

        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
}
